package com.ruoyi.project.system.device.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;

public class ItextPdfHeaderFooter extends PdfPageEventHelper {
    /**
     * 页眉
     */
    public String header ="";
    /**
     * 文档字体大小
     */
    public int persentFontSize = 12;

    public Rectangle pageSize = PageSize.A4;

    public PdfTemplate total;

    public BaseFont bf = null;

    public Font fontDetail = null;

    public ItextPdfHeaderFooter() {
    }

    public ItextPdfHeaderFooter(BaseFont bf,int persentFontSize, Rectangle pageSize) {
        this.persentFontSize = persentFontSize;
        this.pageSize = pageSize;
        this.bf = bf;
    }

    public ItextPdfHeaderFooter(String header, int persentFontSize, Rectangle pageSize) {
        this.header = header;
        this.persentFontSize = persentFontSize;
        this.pageSize = pageSize;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public void setPersentFontSize(int persentFontSize) {
        this.persentFontSize = persentFontSize;
    }

    @Override
    public void onOpenDocument(PdfWriter writer, Document document) {
        total = writer.getDirectContent().createTemplate(60,60);
    }

    /**
     * 写入页脚
     * @param writer
     * @param document
     */
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        //如果有封面第一页跳过，后续计算页码需要减去封面页数
//        if(writer.getPageNumber()==1)
//        {
//            return;
//        }
        try{
            if(fontDetail==null){
                fontDetail = new Font(bf,persentFontSize, Font.NORMAL);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //1、写入页脚
        ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT,new Phrase(header,fontDetail),document.left(),document.top()+20,0);

        //有封面减去封面页数
        //        int pages = writer.getPageNumber()-1;
        int pages = writer.getPageNumber();
        String foot = "第"+pages+"页/共";
        Phrase footer = new Phrase(foot,fontDetail);

        //计算长度位置
        float len = bf.getWidthPoint(foot, persentFontSize);

        PdfContentByte cb = writer.getDirectContent();

        ColumnText.showTextAligned(cb,Element.ALIGN_CENTER,footer,(document.rightMargin()+document.right()+document.leftMargin()-document.left()-len)/2.0F+20F,document.bottom()-20,0);
        cb.addTemplate(total,(document.rightMargin()+document.right()+document.leftMargin()-document.left())/2.0F+20F,document.bottom()-20);
    }

    /**
     * 关闭文档时添加总页码
     * @param writer
     * @param document
     */
    @Override
    public void onCloseDocument(PdfWriter writer, Document document) {
        if(writer.getPageNumber()==1)
        {
            return;
        }
        total.beginText();
        total.setFontAndSize(bf,persentFontSize);
        //有封面减去封面页数
        //        String footT = "" + (writer.getPageNumber()-1)+"页";
        String footT = "" + (writer.getPageNumber())+"页";
        Phrase footer = new Phrase(footT,fontDetail);
        total.showText(footer.getContent());
        total.endText();
        total.closePath();
    }
}